package sk.fiit.icp.eventmanager.view;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.yui.calendar.DateField;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sk.fiit.icp.components.SignInSession;
import sk.fiit.icp.data.Events;
import sk.fiit.icp.data.EventsDao;
import sk.fiit.icp.event.view.EventPanel;

public class EventManagerNavigation extends Panel {
	@SpringBean(name = "eventsDao")
	public EventsDao eventsDao;

	public EventManagerNavigation(String id, final EventPanel panel) {
		super( id );

		add( new RegistrationForm( "searchForm", new CompoundPropertyModel<Events>( new Events() ), panel ) );

	}

	class RegistrationForm extends Form<Events> {

		public RegistrationForm(String id, CompoundPropertyModel<Events> eventModel, final EventPanel panel) {
			super( id, eventModel );
			List<Events> e = eventsDao.findAllByUser( ((SignInSession) getSession()).getUser() );

			AjaxSubmitLink eventButton = new AjaxSubmitLink( "addNewEvent" ) {

				@Override
				protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
					panel.setActualEvent( new Events() );
					target.add( panel );
				}

				@Override
				protected void onError(AjaxRequestTarget target, Form<?> form) {
					target.add( form );
				}
			};
			eventButton.add( new Image( "addNewEventImage", new ContextRelativeResource( "/images/Page/ButtonAddEvent.png" ) ) );
			add( eventButton );

			add( new DateField( "startTime" ) );
			add( new DateField( "endTime" ) );

			ListView<Events> listview = new ListView<Events>( "listview", e ) {
				@Override
				protected void populateItem(final ListItem<Events> item) {
					AjaxLink<String> link = new AjaxLink<String>( "eventLinks" ) {

						@Override
						public void onClick(AjaxRequestTarget target) {
							panel.setActualEvent( item.getModelObject() );
							target.add( panel );
						}
					};
					link.add( new Label( "linkName", item.getModelObject().getTitle() ) );
					item.add( link );
				}

			};
			add( listview );
		}
	}
}
